package org.example.myleet.p1035;

/**
 * 动态规划，最长公共子序列问题
 */
public class Solution {
    public int maxUncrossedLines(int[] nums1, int[] nums2) {
        int m = nums1.length;
        int n = nums2.length;
        int[][] dp = new int[m + 1][n + 1];
        for (int i = 0; i < m; ++i) {
            dp[i][0] = 0;
        }
        for (int j = 0; j < n; ++j) {
            dp[0][j] = 0;
        }
        int num1;
        int num2;
        for (int i = 0; i < m; ++i) {
            num1 = nums1[i];
            for (int j = 0; j < n; ++j) {
                num2 = nums2[j];
                if (num1 == num2) {
                    dp[i + 1][j + 1] = dp[i][j] + 1;
                } else {
                    dp[i + 1][j + 1] = Math.max(dp[i][j + 1], dp[i + 1][j]);
                }
            }
        }
        return dp[m][n];
    }
}
